package com.example.administrator.foodrowfresh.fragmentframework;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.administrator.foodrowfresh.R;
import com.example.administrator.foodrowfresh.configs.classifyconfing.MyClassifyConfing;
import com.example.administrator.foodrowfresh.configs.classifyconfing.classifyadapter.ClassifyDetailItem;
import com.example.administrator.foodrowfresh.configs.classifyconfing.classifyadapter.MyLeftAdapter;
import com.example.administrator.foodrowfresh.configs.classifyconfing.classifyadapter.MyRightAdapter;
import com.example.administrator.foodrowfresh.configs.classifyconfing.classifyentity.MyClassifyLeftEntity;
import com.example.administrator.foodrowfresh.configs.classifyconfing.classifyentity.MyVegetableEntity;
import com.example.administrator.foodrowfresh.superBase.MyBaseCallBack;
import com.example.administrator.foodrowfresh.superBase.MyBaseFragment;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/4/30.
 */
public class Classify extends MyBaseFragment implements AdapterView.OnItemClickListener {
    private View mView;
    private ListView mLeftListView;
    private ListView mRightListView;
    private List<MyClassifyLeftEntity.DataBean.ListBean> mLeftDatas;
    private  MyLeftAdapter myLeftAdapter;
    private List<MyVegetableEntity.DataBean.ListBean> mVegetableDatas;
    private  MyRightAdapter myRightAdapter;
    private List<String> rightPath;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initData();
    }

    @Override
    public View initView() {
        mView = LayoutInflater.from(getActivity()).inflate(R.layout.layout_classify_fm, null, false);
        mLeftListView= (ListView) mView.findViewById(R.id.classify_left_item);
        mRightListView= (ListView) mView.findViewById(R.id.classify_right_item);
        mLeftListView.setAdapter(myLeftAdapter);
        mLeftListView.setOnItemClickListener(this);
        mRightListView.setAdapter(myRightAdapter);
        mRightListView.setOnItemClickListener(this);
        return mView;
    }

    @Override
    public void initData() {
        rightPath=new ArrayList<>();
        mVegetableDatas=new ArrayList<>();
        mLeftDatas=new ArrayList<>();
        //下载左边数据
        getData();
        //给左边的条目设置适配器
        myLeftAdapter=new MyLeftAdapter(mLeftDatas,getContext());

        //下载右边的数据
        getRight(MyClassifyConfing.Right_Vegetable);
        myRightAdapter=new MyRightAdapter(mVegetableDatas,getContext());
        //将右边的点击事件传到gridview中
        myRightAdapter.setListener(this);
        //添加右边条目的路径
        rightPath.add(MyClassifyConfing.Right_Vegetable);
        rightPath.add(MyClassifyConfing.Right_friut);
        rightPath.add(MyClassifyConfing.Right_meat);
        rightPath.add(MyClassifyConfing.Right_fish);
        rightPath.add(MyClassifyConfing.Shu_shi);
        rightPath.add(MyClassifyConfing.Meal_Bread);
        rightPath.add(MyClassifyConfing.Oil_food);
        rightPath.add(MyClassifyConfing.Snack_food);
        rightPath.add(MyClassifyConfing.Import_food);
        rightPath.add(MyClassifyConfing.Kitchen_food);
    }

    public void getData(){
        getData(MyClassifyConfing.LEFT_PATH, new MyBaseCallBack() {
            @Override
            public void onStart() {

            }
            @Override
            public void onResult(String result) {
                Gson gson=new Gson();
                MyClassifyLeftEntity myclassifyLeft=gson.fromJson(result, MyClassifyLeftEntity.class);
                MyClassifyLeftEntity.DataBean data = myclassifyLeft.getData();

                List<MyClassifyLeftEntity.DataBean.ListBean> list = data.getList();

                mLeftDatas.addAll(list);
                myLeftAdapter.notifyDataSetChanged();
            }
            @Override
            public void onError() {
                Toast.makeText(getContext(), "网络访问失败", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onFinish() {

            }
        });
    }
    //获取右边的数据：
    public void getRight(String  path){
        getData(path, new MyBaseCallBack() {
            @Override
            public void onStart() {
             //   Toast.makeText(getContext(), "开始下载", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onResult(String result) {
                Gson gson=new Gson();
                MyVegetableEntity myVegetable=gson.fromJson(result,MyVegetableEntity.class);
                MyVegetableEntity.DataBean data = myVegetable.getData();
                List<MyVegetableEntity.DataBean.ListBean> list = data.getList();
                mVegetableDatas.addAll(list);
                myRightAdapter.notifyDataSetChanged();
            }

            @Override
            public void onError() {
                Toast.makeText(getContext(), "下载失败", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onFinish() {

            }
        });
    }
    //从事复杂的逻辑操作
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        if(parent.getId()==R.id.classify_left_item) {
            myLeftAdapter.getChange(position);
            myLeftAdapter.notifyDataSetChanged();
            String path = rightPath.get(position);
            mVegetableDatas.clear();
            getRight(path);
            myRightAdapter.notifyDataSetChanged();
        }else {
            TextView textView = (TextView) view.findViewById(R.id.grid_id);
            String text=textView.getText().toString();
            TextView textView1= (TextView) view.findViewById(R.id.grid_text);
            String textTitle=textView1.getText().toString();
            Intent intent=new Intent();
            intent.putExtra("id",text);
            intent.putExtra("textTitle",textTitle);
            intent.setClass(getActivity(), ClassifyDetailItem.class);
            startActivity(intent);
        }
    }
}
